package cm309.BookingSystem.web.servlet;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.context.request.WebRequest;
import cm309.BookingSystem.common.Common;
import cm309.BookingSystem.common.JsonDataWrapper;
import cm309.BookingSystem.common.ResponseJson;
import cm309.BookingSystem.web.model.Member;
import cm309.BookingSystem.web.model.MemberService;

@Controller
@RequestMapping(value="/MB")
//@SessionAttributes(value={"member"},types={Member.class})
public class MemberController {
	@Autowired(required=true)
	@Qualifier("memberService")
    private MemberService memberService;
	
	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
    //Get all member List
	@ResponseBody  
	@RequestMapping(value="/ReadMB")
	public List<Member> getAllMemberJson(WebRequest request,  Model model)   
	     {  		
		List<Member> members=memberService.getAll();

	    JsonDataWrapper<Member> jsonDataWrapper = new JsonDataWrapper<Member>(10,1,members);  
	    return jsonDataWrapper.getRows();   
		//return members;
	}
	//Member Login
	@ResponseBody  
	@RequestMapping(value="/Login",method = RequestMethod.POST)
	public ResponseJson<Member> goLogin(@RequestParam(value="memberName",required=true) String memberName
			                   ,@RequestParam(value="password",required=true) String password,
			HttpServletRequest req, HttpServletResponse res,ModelMap modelMap)   
	     {  		
		Common.logger.info("add MemberName/password to session="+memberName+":"+password);
		Member mb=memberService.loginMember(memberName, password);
	
		//****Cannot create a session after the response has been committed//*****
		// modelMap.addAttribute("member",mb);
		
		HttpSession session = req.getSession();
		session.setAttribute("member",mb);
		ResponseJson<Member> responsejson=new ResponseJson<Member>("Login fail!",false,"Error");
		if (mb!=null)
		{//modelMap.addAttribute not null 
		 responsejson.setMessage("Login success!");
		 responsejson.setSuccess(true);
		 responsejson.setTitle("Info");
		 responsejson.setO(mb);
		} 
		
		//return  Common.createJsonResponse(responsejson);
		return responsejson;
	}  
	//Get current login member info
	@ResponseBody  
	@RequestMapping(value="/MyMember")
	//public ResponseEntity<String> getLoginMember(@ModelAttribute("member") Member mb,ModelMap modelMap
	public ResponseEntity<String> getLoginMember(ModelMap modelMap
			,HttpServletRequest req, HttpServletResponse res)   
	     {  		
		Member mb=(Member)req.getSession().getAttribute("member");
		ResponseJson<Object> responsejson=new ResponseJson<Object>("You never Login!",false,"Error");
		if (mb!=null)
		{//modelMap.addAttribute not null 
	     responsejson.setMessage("Login success!");
	     responsejson.setSuccess(true);
	     responsejson.setTitle("Info");
	     responsejson.setO(mb);
		 return  Common.createJsonResponse(responsejson);
		} 
		
		return  Common.createJsonResponse(responsejson);
	}  
	//Get current login member info
	@RequestMapping(value="/Logout")
	public ResponseEntity<String> getLogoutMember(HttpServletRequest req, HttpServletResponse res)   
	     {  		
		req.getSession().removeAttribute("member");
		ResponseJson<Object> responsejson=new ResponseJson<Object>("You Logout success!",true,"Info");
	
		return  Common.createJsonResponse(responsejson);
	}  
	//Get member register
	@RequestMapping(value="/CreateMB")
	public ResponseEntity<String> createMember( @RequestParam(value="memberName",required=true) String memberName
			,@RequestParam(value="loginName",required=true) String loginName
			,@RequestParam(value="password",required=true) String pw
			,@RequestParam(value="memberAge",required=true) int memberAge
			,@RequestParam(value="email",required=true) String email
            ,HttpServletRequest req, HttpServletResponse res,ModelMap modelMap)   
	     {  		
		memberService.createMember(memberName, loginName, pw, memberAge, email);
		ResponseJson<Object> responsejson=new ResponseJson<Object>("You regist success!",true,"Info");
		
		
		return  Common.createJsonResponse(responsejson);
	}


}
